package com.yinhechuangji.application.controller;

import com.yinhechuangji.application.entity.UserBase;
import com.yinhechuangji.application.feign.UserFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;



@RestController()
@RequestMapping()
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private static final Logger logger= LoggerFactory.getLogger(TestController.class);

    @Autowired
    private LoadBalancerClient loadBalancerClient;  //

    @Autowired
    private UserFeignClient userFeignClient;


    @GetMapping("/testConsumer")
    public UserBase testConsumer(){
        return restTemplate.getForObject("http://eureka-service/test",UserBase.class);
    }

    @GetMapping("/testFeign")
    public UserBase testFeign(){
        return userFeignClient.test();
    }

    @GetMapping("/clientInstance")
    public void clientInstance(){
        ServiceInstance serviceInstance=loadBalancerClient.choose("eureka-service");        //从Eureka获取的服务对象,此对象不能和restTemplate.getForObject在同一方法中，这样会导致ribbon不负载均衡
        logger.info("{}:{}:{}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
    }

}
